<?php

/**
 * Created by PhpStorm.
 * User: zhangwei
 * Date: 2017/7/5
 * Time: 22:43
 */

namespace System\Lib;

class RegionLib {

	
	//获取区域列表
	public static function get_region_list($region_ids = 0){
		
		$region_list = array();
		$province_ids = array();
		$city_ids = array();
		$district_ids = array();
		
		if($region_ids && is_array($region_ids)){
			
			foreach ($region_ids as $val){
				$province_ids[]	= $val[0];
				$city_ids[]		= $val[1];
				$district_ids[]	= $val[2];
			}
			//去重复
			$province_ids	= array_unique($province_ids);
			$city_ids		= array_unique($city_ids);
			$district_ids	= array_unique($district_ids);
			
			$region_list['province']	=  field_as_key(M('Region')->where(array('id'=>array('in',$province_ids)))->select(),'id');
			$region_list['city']		=  field_as_key(M('Region')->where(array('id'=>array('in',$city_ids)))->select(),'id');
			$region_list['district']	=  field_as_key(M('Region')->where(array('id'=>array('in',$district_ids)))->select(),'id');
			
			
			return $region_list;
		}
	}
	
	//获取省- 省下的市，市下的县列表
	public static function get_fixed_region_list($info = 0){
		
		$region = array();
		
		if($info && is_array($info)){
			
			$province_id	= $info[0] ? $info[0] : false;
			$city_id		= $info[1] ? $info[1] : false;
			$district_id	= $info[2] ? $info[2] : false;
		}
		
		$province_id	&& $region['province']	=  M('Region')->where(array('parent_id'=>0))->select();
		$province_id	&& $region['city']		=  M('Region')->where(array('parent_id'=>$province_id))->select();
		$city_id		&& $region['district']	=  M('Region')->where(array('parent_id'=>$city_id))->select();
		
		$region['province_id']	= $province_id;
		$region['city_id']		= $city_id;
		$region['district_id']	= $district_id;
		
		return $region;
	}
	
	//获取省-市-县的名称(只适用于调用一次，不能foreach中调用)
	public static function get_site_name($region_ids){

		if($region_ids && is_array($region_ids)){

			$provice	=  M('Region')->field('id,name')->find($region_ids[0]);
			$city		=  M('Region')->field('id,name')->find($region_ids[1]);
			$district	=  M('Region')->field('id,name')->find($region_ids[2]);
		}

		$site = $provice['name'] .'-'.$city['name'].'-'.$district['name'];
		return $site;
	}
	
	//根据id获取区域的名字
	public static function get_region_names($region_ids){
		
		if($region_ids && is_array($region_ids)){
			
			$region_names= M('Region')->field('id,name')->where(array('id' => array('in',$region_ids)))->select();
			return $region_names;
		}else{
			return false;
		}
		
	}
	
}
